﻿namespace DslEditorPowerToy.Controls
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class EditorHostControl : UserControl, IMonitorSelection, ISelectionService
    {
        private Assembly _editorDeclaringAssembly;
        private Guid _editorId;
        private Dictionary<Guid, IHostedEditorDefinition> _editors;
        private IContainer components = null;
        private LeafedHostedEditorControl leafedEditorHost;

        public event EventHandler EditorChanged;

        public event EventHandler SelectionChanged;

        public event EventHandler SelectionChanging;

        public EditorHostControl(Assembly editorDeclaringAssembly, Guid editorId)
        {
            this.InitializeComponent();
            this._editorId = editorId;
            this._editors = new Dictionary<Guid, IHostedEditorDefinition>();
            this._editorDeclaringAssembly = editorDeclaringAssembly;
        }

        public void ActivateEditor(Guid editorId)
        {
            if (this._editors.ContainsKey(editorId))
            {
                this.leafedEditorHost.ActivateEditor(editorId);
            }
        }

        private object CreateInstance(System.Type type)
        {
            object obj2 = null;
            try
            {
                obj2 = Activator.CreateInstance(type);
            }
            catch (Exception)
            {
            }
            return obj2;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public bool GetComponentSelected(object component)
        {
            return this.leafedEditorHost.GetComponentSelected(component);
        }

        public List<IHostedEditorDefinition> GetEditors()
        {
            List<IHostedEditorDefinition> list = new List<IHostedEditorDefinition>();
            foreach (KeyValuePair<Guid, IHostedEditorDefinition> pair in this._editors)
            {
                list.Add(pair.Value);
            }
            return list;
        }

        public ICollection GetSelectedComponents()
        {
            return this.leafedEditorHost.GetSelectedComponents();
        }

        private void InitialiseEditorUI()
        {
            bool flag = true;
            foreach (KeyValuePair<Guid, IHostedEditorDefinition> pair in this._editors)
            {
                if (pair.Value.Control != null)
                {
                    HostedEditor editor = new HostedEditor(pair.Value.Id, pair.Value.Control, pair.Value.Caption, pair.Value.Image);
                    this.leafedEditorHost.AddEditor(editor);
                }
                if (flag)
                {
                    this.leafedEditorHost.ActivateEditor(pair.Value.Id);
                    flag = false;
                }
            }
        }

        private void InitializeComponent()
        {
            this.leafedEditorHost = new LeafedHostedEditorControl();
            base.SuspendLayout();
            this.leafedEditorHost.Dock = DockStyle.Fill;
            this.leafedEditorHost.Location = new Point(0, 0);
            this.leafedEditorHost.Name = "leafedEditorHost";
            this.leafedEditorHost.NavigationStyle = NavigationOrientationStyle.Vertical;
            this.leafedEditorHost.Size = new Size(0x135, 0xf5);
            this.leafedEditorHost.TabIndex = 0;
            this.leafedEditorHost.EditorChanged += new EventHandler(this.leafedEditorHost_EditorChanged);
            this.leafedEditorHost.SelectionChanged += new EventHandler(this.leafedEditorHost_SelectionChanged);
            this.leafedEditorHost.SelectionChanging += new EventHandler(this.leafedEditorHost_SelectionChanging);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.leafedEditorHost);
            base.Name = "MultiEditorHost";
            base.Size = new Size(0x135, 0xf5);
            base.Load += new EventHandler(this.MultiEditorHost_Load);
            base.ResumeLayout(false);
        }

        private void leafedEditorHost_EditorChanged(object sender, EventArgs e)
        {
            this.OnEditorChanged();
        }

        private void leafedEditorHost_SelectionChanged(object sender, EventArgs e)
        {
            this.OnSelectionChanged();
        }

        private void leafedEditorHost_SelectionChanging(object sender, EventArgs e)
        {
            this.OnSelectionChanging();
        }

        private void LoadEditors()
        {
            if (this._editorDeclaringAssembly != null)
            {
                foreach (System.Type type in this._editorDeclaringAssembly.GetTypes())
                {
                    object[] customAttributes = type.GetCustomAttributes(typeof(HostedEditorDefinitionAttribute), false);
                    if (((customAttributes != null) && (customAttributes.GetLength(0) > 0)) && (type.GetInterface(typeof(IHostedEditorDefinition).Name) != null))
                    {
                        object obj2 = this.CreateInstance(type);
                        if (obj2 != null)
                        {
                            IHostedEditorDefinition definition = obj2 as IHostedEditorDefinition;
                            if (definition.Id == this._editorId)
                            {
                                this._editors.Add(definition.Id, definition);
                                return;
                            }
                        }
                    }
                }
            }
        }

        private void MultiEditorHost_Load(object sender, EventArgs e)
        {
            this.LoadEditors();
            this.InitialiseEditorUI();
        }

        public void NotifySelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {
            this.leafedEditorHost.NotifySelectionChanged(selectionProvider, source);
        }

        protected void OnEditorChanged()
        {
            if (this.EditorChanged != null)
            {
                this.EditorChanged(this, new EventArgs());
            }
        }

        protected override void OnGotFocus(EventArgs e)
        {
            this.leafedEditorHost.Focus();
            base.OnGotFocus(e);
        }

        protected void OnSelectionChanged()
        {
            if (this.SelectionChanged != null)
            {
                this.SelectionChanged(this, new EventArgs());
            }
        }

        protected void OnSelectionChanging()
        {
            if (this.SelectionChanging != null)
            {
                this.SelectionChanging(this, new EventArgs());
            }
        }

        protected override bool ProcessTabKey(bool forward)
        {
            return base.SelectNextControl(base.ActiveControl, forward, true, true, true);
        }

        public void SetSelectedComponents(ICollection components)
        {
            this.leafedEditorHost.SetSelectedComponents(components);
        }

        public void SetSelectedComponents(ICollection components, SelectionTypes selectionType)
        {
            this.leafedEditorHost.SetSelectedComponents(components, selectionType);
        }

        public IHostedEditorDefinition CurrentEditor
        {
            get
            {
                HostedEditor currentEditor = this.leafedEditorHost.CurrentEditor;
                if (currentEditor == null)
                {
                    return null;
                }
                if (!this._editors.ContainsKey(currentEditor.Id))
                {
                    return null;
                }
                return this._editors[currentEditor.Id];
            }
        }

        protected Dictionary<Guid, IHostedEditorDefinition> Editors
        {
            get
            {
                return this._editors;
            }
        }

        public NavigationOrientationStyle NavigationStyle
        {
            get
            {
                return this.leafedEditorHost.NavigationStyle;
            }
            set
            {
                this.leafedEditorHost.NavigationStyle = value;
            }
        }

        public object PrimarySelection
        {
            get
            {
                return this.leafedEditorHost.PrimarySelection;
            }
        }

        public int SelectionCount
        {
            get
            {
                return this.leafedEditorHost.SelectionCount;
            }
        }
    }
}

